package com.wy;

import org.junit.Test;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author 33117
 * @version 1.0
 * @data 2025/2/27 18:16
 */
@SpringBootApplication
@MapperScan("com.wy.mapper")
public class Main {
    public static void main(String[] args) {
        SpringApplication.run(Main.class, args);
        System.out.println("111");

    }

    @Test
    public void test1() {
        cc:
        for (int i = 1; i < 10; i++) {
            for (int j = 1; j < 10; j++) {
                if (j == 5) {
                    break cc;
                }
                System.out.println("j=" + j);
            }
        }
    }

    @Test
    public void test2() {
        Integer f1 = 100, f2 = 100, f3 = 150, f4 = 150;
        System.out.println(f1 == f2);
        System.out.println(f3 == f4);
    }

    /*
     * File
     */
    @Test
    public void test3() throws IOException {
        File file = new File("aa\\bb");
        File file1 = new File("aa\\bb\\wy.txt");
        File file2 = new File("aa\\bb\\nihao.txt");
        System.out.println("删除" + file1.delete());
        System.out.println("删除" + file.delete());
        if (!file.isDirectory()) {
            boolean mkdirs = file.mkdirs();
            System.out.println(mkdirs);
        }
        if (!file1.isFile()) {
            file1.createNewFile();
            System.out.println("相对路径" + file1.getName());
            System.out.println("绝对路径" + file1.getAbsolutePath());
        }
        if (!file2.isFile()) {
            file2.createNewFile();
        }
        // 这里输出的结果是 6 因为是 在utf-8中 你3字节 好3字节 所以为6
        System.out.println(file2.length());
    }

    /*
     * I/O流
     * I:输入流指读取数据
     * O:输出流指写入数据
     * 在存储(图片、文本、视频)时以二进制的来保存
     * 字节流和字符流 各位以字节、字符为单位
     * 字节输入流 : InputStream
     * 字节输出流 : OutputStream
     * 字符输入流 : Reader
     * 字符输出流 : Writer
     */
    @Test
    public void test04() {
        /*
         * 字节输出流
         * 给属性没有赋值(否则赋值 可追加)是创建两个文件都有数据
         *  - 一个文件中写入使用代码写入数据更新原有的数据
         *  - 另一个文件没写入数据 则清空数据
         * 在abx.txt中写入了ABCabc
         */
        outputStream();
        // 字节输入流
        inputStream();
        // 案例-字节输入输出流图片
        Tdome();
    }

    private void inputStream() {
        try {
            File file = new File("abc.txt");
            if (!file.exists()) {
                FileInputStream inputStream = new FileInputStream(file);
                int length = Math.toIntExact(file.length());
                byte[] bytes = new byte[length];
                int read = inputStream.read(bytes);
                System.out.println(read);
                System.out.println(new String(bytes));
                inputStream.close();
            }
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private void outputStream() {
        try {
            OutputStream outputStream = new FileOutputStream("abc.txt");
            // 写入
            outputStream.write(65);
            outputStream.write(66);
            outputStream.write(67);
            outputStream.write("abc".getBytes());
            // 强刷
            outputStream.flush();
            // 关闭
            outputStream.close();
            ;
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private void Tdome() {
        // 硬盘-内存-硬盘
        File file = new File("abc.jpg");
        try {
            FileInputStream inputStream = new FileInputStream(file);
            byte[] bytes = new byte[Math.toIntExact(file.length())];
            inputStream.read(bytes);
            inputStream.close();
            FileOutputStream fileOutputStream = new FileOutputStream("bcd.jpg");
            fileOutputStream.write(bytes);
            fileOutputStream.flush();
            fileOutputStream.close();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /*
     * 字符流
     */
    @Test
    public void test5() {
        // 字符输入流
        fileReader();
        // 字符输出流
        fileWriter();
        // 案例视频-输入出输出流
        Tdome2("录屏_20250228_154417.mp4", "录屏_20250228_154417-copyTdome2.mp4");
        // 案例视频-缓存输入出输出流
        Tdome3("录屏_20250228_154417.mp4", "录屏_20250228_154417-copyTdome3.mp4");
    }

    private void fileWriter() {
        File file = new File("a.txt");
        try {
            FileWriter writer = new FileWriter(file);
            writer.write("aaaaa");
            writer.close();
            FileReader reader = new FileReader(file);
            char[] chars = new char[Math.toIntExact(file.length())];
            reader.read(chars);
            System.out.println(new String(chars));
            reader.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private void fileReader() {
        try {
            File file = new File("abc.txt");
            FileReader reader = new FileReader(file);
            char[] chars = new char[Math.toIntExact(file.length())];
            reader.read(chars);
            System.out.println(new String(chars));
            reader.close();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private void Tdome2(String src, String dest) {
        File srcc = new File(src);
        File destt = new File(dest);
        try {
            FileInputStream inputStream = new FileInputStream(srcc);
            FileOutputStream outputStream = new FileOutputStream(destt);
            long time1 = System.currentTimeMillis();
            byte[] bytes = new byte[Math.toIntExact(srcc.length())];
            while (inputStream.read(bytes) != -1) {
                outputStream.write(bytes);
            }
            long time2 = System.currentTimeMillis();
            System.out.println((time2 - time1));
            inputStream.close();
            outputStream.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private void Tdome3(String src, String dest) {
        File srcc = new File(src);
        File destt = new File(dest);
        try {
            FileInputStream inputStream = new FileInputStream(srcc);
            FileOutputStream outputStream = new FileOutputStream(destt);
            BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
            long time1 = System.currentTimeMillis();
            byte[] bytes = new byte[Math.toIntExact(srcc.length())];
            while (bufferedInputStream.read(bytes) != -1) {
                bufferedOutputStream.write(bytes);
            }
            long time2 = System.currentTimeMillis();
            System.out.println((time2 - time1));
            bufferedInputStream.close();
            bufferedOutputStream.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /*
     * 转换流
     */
    @Test
    public void test06() throws IOException {
        File file = new File("abc.txt");
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        // GBK UTF-8
        OutputStreamWriter gbk = new OutputStreamWriter(fileOutputStream, "utf-8");
        gbk.write("你好aaaaaa");
        gbk.close();
        InputStream inputStream = new FileInputStream(file);
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
        char[] chars = new char[Math.toIntExact(file.length())];
        while (inputStreamReader.read(chars) != -1) {
            System.out.println(chars);
        }
        inputStreamReader.close();
    }


    /*
     * 收集日志
     */
    @Test
    public void test07() throws FileNotFoundException {
        try {
            String s = null;
            s.toString();
        } catch (Exception e) {
            PrintWriter writer = new PrintWriter("log.txt");
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            writer.println(simpleDateFormat.format(new Date())+ ":" + e.getMessage());
            writer.close();
        }
    }
}